﻿//------------------------------------------------------------------------------
// System Name:   Customer Service Platform
// Component:     CIS Domestic ProcinceManagementHelper
// Created By:    Samara Jiang (samaraj@wicresoft.com)
// Created Date:  2011-1-30
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business.CIS
{
    /// <summary>
    ///  Province Management模块数据库访问类
    /// </summary>
    public static class ProvinceManagementHelper
    {
        /// <summary>
        /// 通过ProvinceID查询Departments
        /// </summary>
        public static IEnumerable GetDepartmentWithProvinceRelation(int provinceID)
        {
            using (CCEntities dc = new CCEntities())
            {
                var province = dc.Provinces.FirstOrDefault(p => p.ProvinceID == provinceID);
                var department = dc.Departments.Where(p => p.Disabled == false || !p.Disabled.HasValue).ToArray();
                province.Departments.Load();

                var source = from p in department
                             select new
                             {
                                 p.DepartmentID,
                                 p.DepartmentNameCN,
                                 p.DepartmentNameEN,
                                 p.Description,
                                 ContainsProvince = province.Departments.Contains(p) ? "checked" : "",
                             };
                return source.ToArray();
            }
        }



        /// <summary>
        /// 添加Province，当添加CAT时，CAT与现有所有CAT的ID绑定入PCAT表
        /// </summary>
        public static void OnProvinceInserting(string provinceName,string description)
        {
            using (var dc = new CCEntities())
            {
                Province province = new Province();
                province.Name=provinceName;
                province.Description = description;
                dc.AddToProvinces(province);
                dc.SaveChanges();
                var categories = dc.Categories.Where(p => !p.Disabled).ToArray();
                foreach (var cate in categories)
                {
                    PCAT pcat = new PCAT() { Province = province, Category = cate };
                    dc.AddToPCATs(pcat);
                    Role role = new Role();
                    role.IsAdmin = false;
                    role.Name = province.ProvinceID.ToString() + ";" + cate.CateID.ToString();
                    role.Description = "+pcat+";
                    role.BelongsTo = "CID";
                    role.PCATs.Add(pcat);
                    dc.AddToRoles(role);
                    province.PCATs.Add(pcat);
                }
                dc.SaveChanges();
            }

        }

        /// <summary>
        /// 修改Province,和与它对应的Departments
        /// </summary>
        public static void OnProvinceUpdating(ObjectContext dc, Province province, string departmentList)
        {
            province.Departments.Load();
            province.Departments.Clear();
            ProvinceInserting(dc, province, departmentList);
        }

        private static void ProvinceInserting(ObjectContext dc, Province province, string departmentList)
        {
            var departments = dc.CreateQuery<Department>("[Departments]").Where(p => p.Disabled == false || !p.Disabled.HasValue);
            if (!String.IsNullOrEmpty(departmentList))
            {

                foreach (var department in departmentList.Split(','))
                {
                    int departmentID = int.Parse(department);

                    var DepartmenyInProvince = departments.First(p => p.DepartmentID == departmentID);

                    province.Departments.Add(DepartmenyInProvince);

                }

            }

        }
        /// <summary>
        /// 将一个province的激活状态设为未激活
        /// </summary>
        public static void SetProvinceDisabled(int provinceID)
        {
            using (var dc = new CCEntities())
            {
                dc.Provinces.FirstOrDefault(p => p.ProvinceID == provinceID).Disabled = true;
                dc.SaveChanges();
            }
        }

        /// <summary>
        /// 根据当前ID获取Name
        /// </summary>
        public static IEnumerable getName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.Provinces.FirstOrDefault(p => p.ProvinceID == entityID).Name;
                return name;
            }
        }

    }
}
